package com.xxd.spring.boot.demo.lihonghai.interceptor;

import com.xxd.spring.boot.demo.lihonghai.Eeception.MyException;
import com.xxd.spring.boot.demo.lihonghai.constant.ConstantData;
import com.xxd.spring.boot.demo.lihonghai.entity.User;
import com.xxd.spring.boot.demo.lihonghai.uti.UserManageUtil;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class RequestInterceptor extends HandlerInterceptorAdapter {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,Object handler)throws Exception{
        String requestURI=request.getRequestURI();
        if(requestURI.startsWith("/studentSystem/user")){
            return true;
        }
        String authorization = request.getHeader("Authorization");
        User user = ConstantData.userMap.get(authorization);
        if(user == null){
            throw new MyException("没有权限访问,未登录","911");
        }
        UserManageUtil.set(user);
        return true;
    }


  @Override
  public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView)throws Exception
  {   UserManageUtil.remove();
     super.postHandle(request,response,handler,modelAndView);
  }
}